package cn.glenn._08security.base.config;

import cn.glenn._08security.service.UserDetailServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.stereotype.Component;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {
    @Bean
    public UserDetailServiceImpl userDetailsServiceImpl() {
        return new UserDetailServiceImpl();
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                //配置认证策略
                .authorizeHttpRequests(authorize-> authorize
                        //所有/api/public/**路径不需要认证
                        .requestMatchers("/api/public/**").permitAll()
                        //其他请求需要认证
                        .anyRequest().authenticated()
                )
                .formLogin(form->form.permitAll()
                );
        return http.build();
    }
}
